package com.glodon.paas.document.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.glodon.paas.document.DocumentBaseIT;
import com.glodon.paas.exception.NotFoundException;
import com.glodon.paas.exception.PrivilegeException;
import com.glodon.paas.exception.i18n.ErrorCode;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(value = { "classpath:test-document-service.xml", "classpath:paas-document-dao-mysql.xml", })
public class RecyclerServiceMySqlIT extends DocumentBaseIT {

    @Autowired
    private RecyclerService service;

    @Autowired
    private FileService     fileService;

    public void setup() {

    }

    @Test
    public void testRecyclerService() {
        super.cleanShare();
        super.cleanFile();
        super.setup();
        testGetFile();
        testGetFiles();
        testRemove();
        testRestore();
        testClean();
        super.cleanFile();
    }

    public void testGetFile() {
        fileService.remove(ownerId, fileId1);
        try {
            service.getFile(userId, fileId1);
        } catch (PrivilegeException e) {
            assertEquals(ErrorCode.PRIVILEGE_ERROR, e.getErrorCode());
        }
        assertNotNull(service.getFile(ownerId, fileId1));
        service.restore(ownerId, fileId1);
        try {
            service.getFile(ownerId, fileId1);
        } catch (NotFoundException e) {
            assertEquals(ErrorCode.FILE_NOT_EXIST, e.getErrorCode());
        }
    }

    public void testGetFiles() {
        fileService.remove(ownerId, fileId1);
        fileService.remove(ownerId, fileIdPre + 2);
        assertEquals(service.getFiles(ownerId).size(), 2);
        assertEquals(service.getFiles(ownerId, "folder1").size(), 1);
        service.restore(ownerId, fileId1);
        service.restore(ownerId, fileIdPre + 2);
    }

    public void testRemove() {
        fileService.remove(ownerId, fileId1);
        fileService.remove(ownerId, fileIdPre + 2);
        assertEquals(service.getFiles(ownerId).size(), 2);
        service.remove(ownerId, fileIdPre + 2);
        assertEquals(service.getFiles(ownerId).size(), 1);
    }

    public void testRestore() {
        try {
            service.restore(ownerId, fileIdPre + 3);
        } catch (NotFoundException e) {
            assertEquals(ErrorCode.FILE_NOT_EXIST, e.getErrorCode());
        }
        service.restore(ownerId, fileId1);
        try {
            service.restore(ownerId, fileId1);
        } catch (NotFoundException e) {
            assertEquals(ErrorCode.FILE_NOT_EXIST, e.getErrorCode());
        }
    }

    public void testClean() {
        fileService.remove(ownerId, fileId1);
        assertEquals(service.getFiles(ownerId).size(), 1);
        service.clean(ownerId);
        assertEquals(service.getFiles(ownerId).size(), 0);
    }

}
